import {fetchNewsListReq, fetchSinaNewsListReq, fetchStarRankListNoTokenReq} from "../apis/goApis";
import {handleResp} from "../utils/commonfunc";
import {batchCallApiRequest} from "../utils/api";
import {fetchNanoGameListReq} from "../apis/nanoApi";


const gameInterface = {
    matchId: '',
    date: '',
    schedule: '',
    homeTeamName: '',
    awayTeamName: '',
    time: '',
    status: '',
    homeScore: '',
    awayScore: '',
    matchGroupId: '',
    matchGroupType: '',
    competitionId: '',
    competitionName: '',
}


export const home = {

    state: {
        newsList: [],
        sinaNewsList: [],
        gameList: {
            matches: [

            ],

            events: {
                0: {
                    "id": 24, //赛事id
                    "name_zh": "", //中文名称
                    "short_name_zh": "", //中文简称
                    "name_zht": "", //粤语名称
                    "short_name_zht": "", //粤语简称
                    "name_en": "", //英文名称
                    "short_name_en": "", //英文简称
                    "logo": "" // logo,url前缀:http://cdn.sportnanoapi.com/football/competition/
                },
            },

            teams: {
                0: {
                    id: 0, //球队id
                    "name_zh": "", //中文名称
                    "name_zht": "", //粤语名称
                    "name_en": "FC Barcelona", //英文名称
                    "logo": "d159737531127e403115903764cb5145.png", //logo,url前缀:http://cdn.sportnanoapi.com/football/team/
                    "matchevent_id": 120, //赛事id
                },
            },

            stages: {
                0: {
                    "id": 0, //id
                    "mode": 0, //比赛方式, 1-积分 2-淘汰
                    "group_count": 0, //总分组数,0表示没有分组
                    "round_count": 0,//总轮数,0表示没有轮次
                    "name_zh": "", //中文名称
                    "name_zht": "", //粤语名称
                    "name_en": "Groups" //英文名称
                },
            },
        },

        starList: [],
    },

    reducers: {

        setNewsList(state, payload) {
            const {newsList,} = payload
            const l = state.newsList.length
            newsList.forEach((n, i) => {
                n.id = i + l
            })
            // console.log(newsList)
            return {
                ...state,
                newsList: [
                    ...state.newsList,
                    ...newsList,
                ],
            }
        },

        setSinaNewsList(state, payload) {
            const {sinaNewsList,} = payload
            return {
                ...state,
                sinaNewsList,
            }
        },

        setGameList(state, payload) {

            const {
                teams,
                events,
                matches,
                stages,
                odds,
            } = payload

            return {
                ...state,
                gameList: {
                    teams,
                    events,
                    matches,
                    stages,
                    odds,
                },
            }
        },

        setStarList(state, payload) {
            return {
                ...state,
            }
        }

    },

    effects: (dispatch) => ({

        async doFetchNewsList(payload = {startId: 0, endId: 12,}, rootState) {
            // const resp = await fetchNewsListReq({
            //     catalog: 1,
            // })
            // handleResp(resp, dispatch, (data) => {
            //     if (data.ok) {
            //         // console.log(data.ok)
            //         const newsList = data.ok.articles
            //         dispatch.home.setNewsList({
            //             newsList,
            //         })
            //     }
            // })


            const resp = await fetchNewsListReq(payload)
            handleResp(resp, dispatch, (data) => {

                if (data.ok) {
                    // console.log(data.ok)
                    const newsList = data.ok.data
                    dispatch.home.setNewsList({
                        newsList,
                    })
                }
            })
        },

        async doFetchSinaNewsList(payload, rootState) {
            const resp = await fetchSinaNewsListReq()
            handleResp(resp, dispatch, (data) => {
                const sinaNewsList = data.Data
                dispatch.home.setSinaNewsList({
                    sinaNewsList,
                })
            })
        },

        async doFetchGameList(payload, rootState) {
            const date = rootState.game.today

            const resp = await fetchNanoGameListReq({
                date,
            })

            handleResp(resp, dispatch, (data) => {
                if (data.ok) {
                    const dataObject = JSON.parse(data.ok)
                    const teams = dataObject.teams
                    const events = dataObject.events
                    const matches = dataObject.matches
                    const stages = dataObject.stages
                    const odds = dataObject.odds


                    dispatch.home.setGameList({
                        date,
                        teams,
                        events,
                        matches,
                        stages,
                        odds,
                    })
                }
            })

        },

        async doFetchStarRankListNoToken(payload, rootState) {
            const resp = await fetchStarRankListNoTokenReq({t: 1,})
            handleResp(resp, dispatch, (data) => {})
        },

        async doFetchHomeData(payload, rootState) {
            const today = rootState.game.today

            const respList = await batchCallApiRequest([
                fetchNewsListReq({catalog: 1,}),
                fetchNanoGameListReq({date: today,}),
                fetchStarRankListNoTokenReq({t: 1,}),
            ])

            for (let i = 0; i < respList.length; i++) {
                let resp = respList[i]
                let url = resp.config.url
                const newsListUrl = 'http://123.207.85.242/try/go/newslist'
                const gameListUrl = 'http://123.207.85.242/try/go/gameproxy'
                const starRankListUrl = 'http://123.207.85.242/try/go/getstarranklist'

                if (url === newsListUrl) {
                    handleResp(resp, dispatch, (data) => {
                        if (data.ok) {
                            // console.log(data.ok)
                            const newsList = data.ok.articles
                            dispatch.home.setNewsList({
                                newsList,
                            })
                        }
                    })
                }

                if (url === gameListUrl) {

                    handleResp(resp, dispatch, (data) => {
                        if (data.ok) {
                            const dataObject = JSON.parse(data.ok)

                            const teams = dataObject.teams
                            const events = dataObject.events
                            const matches = dataObject.matches
                            const stages = dataObject.stages

                            dispatch.home.setGameList({
                                date: today,
                                teams,
                                events,
                                matches,
                                stages,
                            })

                        }
                    })
                }

                if (url === starRankListUrl) {
                    handleResp(resp, dispatch, (data) => {
                    })
                }
            }
        }


    })

}
